package com.zk.algorithm.backtracking;

import com.zk.algorithm.annotation.LeetCodeExplore;
import com.zk.algorithm.annotation.Medium;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Medium
@LeetCodeExplore
public class Subsets {

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        backtrack(list, new ArrayList<>(), nums, 0);
        return list;
    }

    private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int start) {
        list.add(new ArrayList<>(tempList));

        for (int i = start; i < nums.length; i++) {
            // ========================
            // 末尾添加
            // ========================
            tempList.add(nums[i]);
            // ========================
            // 回溯
            // ========================
            backtrack(list, tempList, nums, i + 1);
            // ========================
            // 末尾去除
            // ========================
            tempList.remove(tempList.size() - 1);
        }
    }

}
